﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using DG.Tweening;
using Resoure.Script.Utilities;
using SciptableObject;
using Unity.Mathematics;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;
using Utilities;
using EventHandler = Utilities.EventHandler;
using Random = UnityEngine.Random;
using SoundName=Utilities.SoundName;

namespace UI
{
    public class CardListController : MonoBehaviour//, IPointerClickHandler, IPointerExitHandler
    {
        //public List<SkillOs> skillList = new List<SkillOs>();
        public bool EnablePrew;
        [Header("卡牌对象引用")] public List<GameObject> prefabsList = new List<GameObject>();
        public List<Card> cardList = new List<Card>();
        [Header("卡槽位置")]
        public List<Vector3> cardSlot;
        public List<bool> isCardSlotEmpty;  
        public bool isChange;
        public float currenPrewTime;
        public float prewTime;
        public Vector3 prewOffset;

        public bool isDrag;
        

        
        private Coroutine previewCoroutine;
        

        public void OnEnable()
        {
            EventHandler.ClearingCardsSlot += ClearingCardsSlot;
        }

        public void Start()
        {
            //↓↓↓ 6张写在for循环条件内了 ↓↓↓
            #region  开局给卡
            for (int i = 0; i < 6; i++)
            {
                ExtractCards();
            }
            #endregion  
            
        }

        public void Update()
        {
            if (!isDrag)
            {
                if (Input.GetKeyDown(KeyCode.Tab))
                {
                    EnablePrew = !EnablePrew;
                    OrganizeCards();
                    if (EnablePrew)
                    {
                        ActivatePre();
                    }
                    else
                    {
                        InActivatePre();
                    }
                }
            }

            //遍历子对象获取卡牌
            cardList = GetComponentsInChildren<Card>(true).ToList();

        }

        /// <summary>
        /// 抽卡
        /// </summary>
        public void ExtractCards()
        {
            for (int i=0;i< 6; i++)
                if (!isCardSlotEmpty[i])
                {
                    // 实例化卡牌对象  
                    var card = Instantiate(prefabsList[Random.Range(0, prefabsList.Count)], this.transform);
                    Card cardC = card.GetComponent<Card>();
                    cardC.cardIndex = i;//标记卡牌索引
                    //计算目标位置,并使用DOTWEEN过渡动画
                    Vector3 targetPosition = !EnablePrew ? cardSlot[i] : cardSlot[i] + prewOffset; 
                    card.GetComponent<RectTransform>().DOAnchorPos(targetPosition, 0.5f).SetEase(Ease.OutCubic);
                    //判断是否处在卡槽预览期（卡牌是否可选）
                    if(currenPrewTime>0)cardC.StartPreview();
                    //标记卡槽占位bool
                    isCardSlotEmpty[i] = true;
                    
                    EventHandler.CallPlaySoundEvent(SoundName.getCard);
                    return;  
                        
                }
        }
        /// <summary>
        /// 整理卡牌
        /// </summary>    
        private void OrganizeCards()
        {
            for (int i = 0; i < isCardSlotEmpty.Count; i++)
            {
                isCardSlotEmpty[i] = false;
            }
            //整理卡牌
            for (int i = 0; i < cardList.Count; i++)
            {
                Vector3 targetPosition = !EnablePrew ? cardSlot[i] : cardSlot[i] + prewOffset;
                cardList[i].GetComponent<RectTransform>().DOAnchorPos(targetPosition, 0.5f).SetEase(Ease.OutCubic);
                cardList[i].cardIndex = i;
                isCardSlotEmpty[i] = true;
            }
        }
        /// <summary>
        /// 接受卡牌释放广播并重赋卡牌占用bool表的值
        /// </summary>
        /// <param name="index"></param>
        public void ClearingCardsSlot(int index)
        {
            isCardSlotEmpty[index] = false;
        }

        // public void OnPointerClick(PointerEventData eventData)
        // {
        //     Debug.Log("启用预览");
        //     currenPrewTime = prewTime;
        //     ActivatePre();
        // }
        /// <summary>
        /// 激活卡槽区预览
        /// </summary>
        public void ActivatePre()
        {
            for (int i = 0; i < cardList.Count; i++)
            {
                cardList[i].StartPreview();
            }
            isChange = true; 
        }

        // public void OnPointerExit(PointerEventData eventData)
        // {
        //     currenPrewTime = prewTime;
        // }
        /// <summary>
        /// 关闭卡槽区预览
        /// </summary>
        public void InActivatePre()
        {
            for (int i = 0; i < cardList.Count; i++)
            {
                cardList[i].StopPreview();
            }

            isChange = true;
        }
        
    }
}